package com.yanqu.road.dao.impl.activity.pawnshop;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.pawnshop.*;
import com.yanqu.road.utils.string.StringUtils;

public class ConfPawnshopDaoImpl extends TempDao {

	// ========================================================================
	private static String getConfPawnshopSql = "select * from `t_s_activity_pawnshop` where `activityId`=? ORDER BY `level` ASC";

	public List<ConfPawnshop> getConfPawnshopList(int activityId) {
		List<ConfPawnshop> list = new ArrayList<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(getConfPawnshopSql);
			int i = 1;
			call.setInt(i++, activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				ConfPawnshop confPawnshop = new ConfPawnshop();
				confPawnshop.setActivityId(rs.getInt("activityId"));
				confPawnshop.setLevel(rs.getInt("level"));
				confPawnshop.setIcon(rs.getString("icon"));
				confPawnshop.setUpgradeMoney(rs.getLong("upgradeMoney"));
				confPawnshop.setUpgradeReward(rs.getLong("upgradeReward"));
				confPawnshop.setStaminaCost(rs.getInt("staminaCost"));
				confPawnshop.setItemLvWeight(rs.getString("itemLvWeight"));
				confPawnshop.setGemstoneLvWeight(rs.getString("gemstoneLvWeight"));
				confPawnshop.setItemCount(rs.getInt("itemCount"));
				confPawnshop.setMoneyRange(rs.getString("moneyRange"));
				confPawnshop.setMoneyIcon(rs.getString("moneyIcon"));
				confPawnshop.setMoneyTriggerBound(rs.getLong("moneyTriggerBound"));
				confPawnshop.setRefusePower(rs.getString("refusePower"));
				confPawnshop.setUpgradeOtherReward(rs.getString("upgradeOtherReward"));
				List<Integer> buyNpcLv = StringUtils.stringToIntegerList(rs.getString("buyNpcLv"), "\\|");
				confPawnshop.setBuyNpcLv(buyNpcLv);
				List<Integer> sellNpcLv = StringUtils.stringToIntegerList(rs.getString("sellNpcLv"), "\\|");
				confPawnshop.setSellNpcLv(sellNpcLv);

				list.add(confPawnshop);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return list;
	}

	// ========================================================================
	private static String getConfPawnshopStaffSql = "select * from `t_s_activity_pawnshop_staff` where `activityId`=?";

	public List<ConfPawnshopStaff> getConfPawnshopStaffList(int activityId) {
		List<ConfPawnshopStaff> list = new ArrayList<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(getConfPawnshopStaffSql);
			int i = 1;
			call.setInt(i++, activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				ConfPawnshopStaff confPawnshopStaff = new ConfPawnshopStaff();
				confPawnshopStaff.setActivityId(rs.getInt("activityId"));
				confPawnshopStaff.setId(rs.getInt("id"));
				confPawnshopStaff.setType(rs.getInt("type"));
				confPawnshopStaff.setLevel(rs.getInt("level"));
				confPawnshopStaff.setName(rs.getString("name"));
				confPawnshopStaff.setUnlockLevel(rs.getInt("unlockLevel"));
				confPawnshopStaff.setParam1(rs.getInt("param1"));
				confPawnshopStaff.setParam2(rs.getInt("param2"));
				confPawnshopStaff.setParamWeight(rs.getString("paramWeight"));
				list.add(confPawnshopStaff);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return list;
	}

	// ========================================================================
	private static String getConfPawnshopItemSql = "select * from `t_s_activity_pawnshop_item` where `activityId`=? ORDER BY `id` ASC";

	public List<ConfPawnshopItem> getConfPawnshopItemList(int activityId) {
		List<ConfPawnshopItem> list = new ArrayList<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(getConfPawnshopItemSql);
			int i = 1;
			call.setInt(i++, activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				ConfPawnshopItem confPawnshopItem = new ConfPawnshopItem();
				confPawnshopItem.setActivityId(rs.getInt("activityId"));
				confPawnshopItem.setId(rs.getInt("id"));
				confPawnshopItem.setName(rs.getString("name"));
				confPawnshopItem.setDesc(rs.getString("desc"));
				confPawnshopItem.setIcon(rs.getString("icon"));
				confPawnshopItem.setType(rs.getInt("type"));
				confPawnshopItem.setLevelRange(rs.getString("levelRange"));
				confPawnshopItem.setWeight(rs.getInt("weight"));
				confPawnshopItem.setParam(rs.getString("param"));
				confPawnshopItem.setCharTextNum(rs.getInt("charTextNum"));
				list.add(confPawnshopItem);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return list;
	}

	// ========================================================================
	private static String getConfPawnshopItemLvSql = "select * from `t_s_activity_pawnshop_item_lv` where `activityId`=?";

	public List<ConfPawnshopItemLv> getConfPawnshopItemLvList(int activityId) {
		List<ConfPawnshopItemLv> list = new ArrayList<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(getConfPawnshopItemLvSql);
			int i = 1;
			call.setInt(i++, activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				ConfPawnshopItemLv confPawnshopItemLv = new ConfPawnshopItemLv();
				confPawnshopItemLv.setActivityId(rs.getInt("activityId"));
				confPawnshopItemLv.setLevel(rs.getInt("level"));
				confPawnshopItemLv.setPriceBase(rs.getString("priceBase"));
				confPawnshopItemLv.setEventAffixRate(rs.getInt("eventAffixRate"));
				confPawnshopItemLv.setPersonAffixRate(rs.getInt("personAffixRate"));
				confPawnshopItemLv.setRarityWeight(rs.getString("rarityWeight"));
				confPawnshopItemLv.setStatusWeight(rs.getString("statusWeight"));
				confPawnshopItemLv.setMoneyIcon(rs.getString("moneyIcon"));
				list.add(confPawnshopItemLv);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return list;
	}

	// ========================================================================
	private static String getConfPawnshopEventSql = "select * from `t_s_activity_pawnshop_event` where `activityId`=?";

	public Map<Integer, ConfPawnshopEvent> getConfPawnshopEventMap(int activityId) {
		Map<Integer, ConfPawnshopEvent> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(getConfPawnshopEventSql);
			call.setInt(1, activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				ConfPawnshopEvent data = new ConfPawnshopEvent();
				data.setActivityId(rs.getInt("activityId"));
				data.setEventId(rs.getInt("eventId"));
				data.setAddition(rs.getString("addition"));
				data.setDuring(rs.getInt("during"));
				data.setWeight(rs.getInt("weight"));
				dataMap.put(data.getEventId(), data);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}
